<?php
/**
* wapinfo.php
*
* Create on 2015-6-8
* Create by TT
*/
namespace haibao\cms\business;

use \haibao\cms\data\WapInfo as WapInfoData;
use \haibao\cms\data\WapInfoHistory as WapInfoHistoryData;

class WapInfo extends \haibao\cms\business\Base {
	/**
	 * 查询文章或图集
	 *
	 * @param \haibao\cms\model\filter\WapInfo $wapInfoFilter
	 */
	public static function getInfoList($wapInfoFilter){
		$data = new WapInfoData();
		return $data->getAll($wapInfoFilter);
	}
	public static function getOneInfo($wapInfoFilter){
		$data = new WapInfoData();
		return $data->getOne($wapInfoFilter);
	}
	
	public static function updateWapInfo($id,$arrSet){
		self::checkOperation(self::FUNC_CODE_M_ARTICLE_LIST);
		self::addWapInfoHistoryModel($id, $arrSet,\haibao\cms\model\data\WapInfoHistory::OPER_TYPE_EDIT);
		if (!$arrSet['Title'] || !$arrSet['PublishTime']){
			throw new BusinessException('名称与发布时间不能为空');
		}
		
		$data = new WapInfoData();
		$wapInfo = $data->getOneById($id);
		$data->updateById($id,$arrSet);
		
		self::saveOperateHistory(self::FUNC_CODE_M_ARTICLE_LIST, array_merge($arrSet,array('id'=>$id)));
	}
	
	public static function delWapInfo($id,$infoType){
		self::checkOperation(self::FUNC_CODE_M_ARTICLE_LIST);
		if (!$id){
			throw new BusinessException('Id不能为空');
		}
		$wapInfoData = new WapInfoData();
		self::addWapInfoHistoryModel($id,array(),\haibao\cms\model\data\WapInfoHistory::OPER_TYPE_DELETE);
		$arrSetToPC = array("IsPostMobile"=>\haibao\cms\model\data\Article::PUBLISH_MOBILE_NO);
		if($infoType == \haibao\cms\model\data\WapInfo::INFO_TYPE_ARTICLE){
			$infoData = new \haibao\cms\data\Article();
		}else{
			$infoData = new \haibao\cms\data\imagecollective\ImageCollective();
		}
		$wapInfo = $wapInfoData->getOneById($id);
		$infoData->updateById($wapInfo->InfoId, $arrSetToPC);
		$wapInfoData->removeById($id);
		$arrSet['Id'] = $id;
		$arrSet['CreateBy'] = self::getCurrentUser ()->Id;
		$arrSet['UpdateTime'] = new \DateTime();
		self::saveOperateHistory(self::FUNC_CODE_M_ARTICLE_LIST, $arrSet);
	}
	
	public static function publishAll($idArr){
		self::checkOperation(self::FUNC_CODE_M_ARTICLE_LIST);
		if(!$idArr){
			throw new BusinessException('发布发生错误');
		}
		$wapInfoData = new WapInfoData();
		$wapInfoData->publishAll($idArr);
		self::saveOperateHistory(self::FUNC_CODE_M_ARTICLE_LIST, $idArr);
	}
	public static function getWapInfoById($id, $type = \haibao\cms\model\data\WapInfo::INFO_TYPE_ARTICLE){
		
		$filter = new \haibao\cms\model\filter\WapInfo();
		$filter->select(array(
				\haibao\cms\model\filter\WapInfo::CONDITION_FIELD_NAME_TITLE,
				\haibao\cms\model\filter\WapInfo::CONDITION_FIELD_NAME_PUBLISH_TIME,
				\haibao\cms\model\filter\WapInfo::CONDITION_FIELD_NAME_TAG_ID,
		));
		$filter->where(\haibao\cms\model\filter\WapInfo::CONDITION_FIELD_NAME_INFO_ID,\haibao\frame\data\query\Condition::CONDITION_EQUAL,$id);
		$filter->where(\haibao\cms\model\filter\WapInfo::CONDITION_FIELD_NAME_INFO_TYPE,\haibao\frame\data\query\Condition::CONDITION_EQUAL,$type);
		$data = new WapInfoData();
		return $data->getOne($filter);
	}
	
	private static function addWapInfoHistoryModel($Id,$model=array(),$operType=\haibao\cms\model\data\WapInfoHistory::OPER_TYPE_EDIT){
		$articleData = new WapInfoHistoryData();
		$historyModel = self::getHistoryModelById($Id, $model,$operType);
		$articleData->add($historyModel);
	}
	
	private static function getHistoryModelById($Id,$model,$operType){
		$historyModel = new \haibao\cms\model\data\WapInfoHistory();
		
		$historyModel->CreateBy = self::getCurrentUser ()->Id;
		$historyModel->CreateTime = new \DateTime();
		$wapInfoData = new WapInfoData();
		$wapInfo = $wapInfoData->getOneById($Id);
		$historyModel->Title = isset($model['Title'])?$model['Title']:$wapInfo->Title;
		$historyModel->IndexTop = isset($model['IndexTop'])?$model['IndexTop']:$wapInfo->IndexTop;
		$historyModel->ChannelTop = isset($model['ChannelTop'])?$model['ChannelTop']:$wapInfo->ChannelTop;
		$historyModel->PublishTime = isset($model['PublishTime'])?$model['PublishTime']:$wapInfo->PublishTime;;
		$historyModel->InfoId = $wapInfo->InfoId;
		$historyModel->InfoType = $wapInfo->InfoType;
		$historyModel->OperType = $operType;
		return $historyModel;
	}
	
}